package com.example.zsr.akpautoupdatedemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zsr.akpautoupdatedemo.FileUtils.FileInfo;
import com.example.zsr.akpautoupdatedemo.download.DownloadService;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;


public class MainActivity extends Activity {
	private static String APKURL = "http://www.infzm.com/mobilesource/goal/app.config.php"; 
	private int localVersionCode = 0;
	private String localVersionName = null;
	private ProgressBar mMyBar;

	private TextView text_status,text_time,text_progress;
	private ImageView btn_pause;
	private String time;
	private FileInfo fileInfo;
	private boolean isPause = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mMyBar = (ProgressBar)findViewById(R.id.app_bar);
		text_status = (TextView)findViewById(R.id.app_bar_text);
		text_time = (TextView)findViewById(R.id.app_time);
		text_progress = (TextView)findViewById(R.id.app_bar_length);
		btn_pause = (ImageView) findViewById(R.id.app_pause);
		
		btn_pause.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!isPause) {
					isPause = true;
					btn_pause.setImageResource(R.mipmap.start);
					Intent btn_stop = new Intent(MainActivity.this,DownloadService.class);
					btn_stop.putExtra("status", "STOP");
					startService(btn_stop);
					
				}else{
					isPause = false;
					btn_pause.setImageResource(R.mipmap.pause);
					Intent btn_start = new Intent(MainActivity.this,DownloadService.class);
					btn_start.putExtra("fileinfo", fileInfo);
					btn_start.putExtra("status", "GO");
					startService(btn_start);
					
				}
			}
		});
		
		// 更新进度
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.msg.apk_install_progress");
		registerReceiver(InstallApkReceiver, filter);
		
		
		
		SimpleDateFormat sDateFormat = new SimpleDateFormat("hh:mm");
		time = sDateFormat.format(new java.util.Date());
		
		fileInfo = new FileInfo(0, "http://images.infzm.com/mobile/infzmreader_release_v5.1.0.apk",
				"northNews.apk", 0, 0);
	}
	
	
	BroadcastReceiver InstallApkReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			// TODO Auto-generated method stub
			String action = arg1.getAction();
			if (action.equals("com.msg.apk_install_progress")) {
				int progress = arg1.getIntExtra("progress",0);

				mMyBar.setProgress(progress);
				text_status.setText("正在下载: "+progress+"%");
				text_time.setText(time);
				
				float curProgress=(float)arg1.getIntExtra("mFinished", 0)/(1024*1024);
				float progressLength = (float)arg1.getIntExtra("length", 0)/(1024*1024);
				DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
				String mCurProgress = decimalFormat.format(curProgress);//format 返回的是字符串
				String mProgressLength = decimalFormat.format(progressLength);
				
				text_progress.setText(mCurProgress+" / "
				+mProgressLength+"M");
			}
		}
	};
	
	
	public void update(View v){

		new CheckLocalInfo(MainActivity.this).execute(APKURL);
	}

	
	public void stop(View v){
		Intent intent = new Intent(this,DownloadService.class);
		intent.putExtra("status", "STOP");
		startService(intent);
	}
	public void go(View v){
		
		Intent intent = new Intent(this,DownloadService.class);
		intent.putExtra("fileinfo", fileInfo);
		intent.putExtra("status", "GO");
		startService(intent);
	}
}
